package models
type SystemType struct {
	Id          int       `json:"id" xorm:"not null pk autoincr comment('主键') INT(11)"`
	Name        string    `json:"name" xorm:"not null comment('题型') VARCHAR(100)"`

}
var systemtype = "system_type"



func  (s *SystemType) GetAll() ([]SystemType,error) {

	var systemtypes []SystemType
	err:=mEngine.Find(&systemtypes)
	return systemtypes,err
}

func  (s *SystemType) GetBySubjectId(subjectId int) ([]SystemType,error) {

	var systemtypes []SystemType

	err := mEngine.Table(systemtype).Distinct(systemtype+".*").
		Join("INNER", systemsubjecttype, systemsubjecttype+".type_id= "+systemtype+".id").
		Where(systemsubjecttype+".subject_id = ?", subjectId).
		Find(&systemtypes)
	return systemtypes,err
}

func (t *SystemType) Add(subjectIdsArr[] int) (int ,error) {
	session := mEngine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		// if returned then will rodefer session.Close()llback automatically
		return 0, err
	}
	//var uid int64
	_, err := session.Insert(t)

	if err != nil {
		return 0, err
	}


	for _,subjectId:=range subjectIdsArr {
		subjectModel := SystemSubject{}
		subjectModel.Id = subjectId
		has := subjectModel.GetRow()
		if !has {
			//如果学科不存在，忽略
			continue
		}

		subjectTypeModel := SystemSubjectType{SubjectId: subjectModel.Id, TypeId: t.Id}

		has, err := session.Get(&subjectTypeModel)
		if err != nil {
				return 0, err
			}
		//如果学科题型不存在，添加学科题型
		if !has {
			_, err = session.Insert(&subjectTypeModel)
			if err != nil {
					return 0, err
				}
		}


	}
	return t.Id,session.Commit()
}

func (t *SystemType) Update(subjectIdsArr[] int) (int ,error) {
	session := mEngine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		// if returned then will rodefer session.Close()llback automatically
		return 0, err
	}
	//var uid int64
	_, err :=mEngine.Where("id = ?", t.Id).Update(t)


	if err != nil {
		return 0, err
	}
	subjectTypeModelForDelete := SystemSubjectType{TypeId: t.Id}
	session.Delete(subjectTypeModelForDelete)

	for _,subjectId:=range subjectIdsArr {
		subjectModel := SystemSubject{}
		subjectModel.Id = subjectId
		has := subjectModel.GetRow()
		if !has {
			//如果学科不存在，忽略
			continue
		}

		subjectTypeModel := SystemSubjectType{SubjectId: subjectModel.Id, TypeId: t.Id}


		_, err = session.Insert(&subjectTypeModel)
		if err != nil {
				return 0, err
			}



	}
	return t.Id,session.Commit()
}

func (t *SystemType) Delete() error {

	//TO: 将彻底删除所有相关的试题

	session := mEngine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		// if returned then will rodefer session.Close()llback automatically
		return err
	}
	/*
	//删除学科题型信息
	subjectTypeModel:=SystemSubjectType{}
	subjectTypeModel.TypeId=t.Id
	if _, err := mEngine.Where("type_id = ?", t.Id).Delete(subjectTypeModel); err != nil {
		return err

	}

	//删除试题相关信息（在数据库中设置了及联删除
	if _, err := mEngine.Exec("DELETE  from "+systemquestion +" WHERE type_id=?" ,t.Id); err != nil {
		return err
	}
	*/
	//删除题型信息
	if _, err := mEngine.Where("id = ?", t.Id).Delete(t); err != nil {
		return err

	}
	return session.Commit()

}
